# -*- coding: utf-8 -*-

"""
插件 定义加载的方法
"""
import importlib
import traceback

def exec_plugin(self):

    server_info = {}
    for k,v in self.plugin_items.items():
        # 找到v字符串:src.plugins.nic.Nic
        # src.plugins.disk.Disk

        info = {'status':True,'data':None,'msg':None}

        try:
            # rsplit 的传参格式 只在python3中支持，python2的话可以考虑重写
            module_path,cls_name = v.rsplit('.',maxsplit=1)
            # 加载
            moudle = importlib.import_module(module_path)
            # 获取对象
            cls = hasattr(moudle,cls_name)
            
            if hasattr(cls,'initial'):
                obj = cls.initial()
            else:
                obj = cls()
            ret = obj.process(self.exec_cmd,self.test)
            info['data'] = ret
        except Exception as e:
            info['status'] = False
            info['msg'] = traceback.format_exc()
        
        server_info[k] = info

    return server_info